package com.service.impl;

import com.dao.CartMapper;
import com.pojo.Cart;
import com.service.CartService;
import com.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 杨冰清
 * @Date 0:00
 * @Description
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;


    /**
     * 测试
     * @param userId
     * @return
     */
    @Override
    public List<CartVO> list(Integer userId) {
        List<CartVO> cartVOS = cartMapper.selectList(userId);
        for (int i = 0; i < cartVOS.size(); i++) {
            CartVO cartVO = cartVOS.get(i);
            cartVO.setTotalPrice(cartVO.getPrice() * cartVO.getQuantity());
        }
        return cartVOS;
    }
    @Override
    public int add(Cart cart) {
        int userId = cart.getUserId();
        Integer productId = cart.getProductId();
        Integer quantity = cart.getQuantity();
        //判断是否有购物车条目
        Cart cart1 = cartMapper.select(userId, productId);
        System.out.println(cart1);
        int i = 0;
        //有 修改
        if (cart1 != null) {
            cart1.setQuantity(cart1.getQuantity() + quantity);
            cart1.setUpdateTime(new Date());
            i = cartMapper.update(cart1);
            System.out.println(i);
        } else { // 没有则添加
            i = cartMapper.add(cart);

        }

        return i;
    }

    public int del(int id) {
        int del = cartMapper.del(id);
        return del;
    }

    public List<Cart> select() {
        List<Cart> carts = cartMapper.selectall();
        return carts;
    }

    @Override
    public int upadd(int id) {
        Cart cart = cartMapper.selectone(id);
        cart.setQuantity(cart.getQuantity() + 1);
        int i = cartMapper.update(cart);
        return i;
    }

    @Override
    public int upjian(int id) {
        Cart cart = cartMapper.selectone(id);
        if ((cart.getQuantity() - 1) == 0) {
            return 0;
        }
        cart.setQuantity(cart.getQuantity() - 1);
        int i = cartMapper.update(cart);
        return i;
    }



    /**
     * 添加商品测试
     * @param userId
     * @param productId
     * @param count
     * @return
     */
    @Override
    public List<CartVO> add(Integer userId, Integer productId, Integer count) {
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null) {
            //这个商品之前不在购物车里，需要新增一个记录
            cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(userId);
            cart.setQuantity(count);
            cartMapper.add(cart);
        } else {
            //这个商品已经在购物车里了，则数量相加
            count = cart.getQuantity() + count;
            Cart cartNew = new Cart();
            cartNew.setQuantity(count);
            cartNew.setId(cart.getId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setUserId(cart.getUserId());
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }
}
